package com.lst.utils;

import com.alibaba.fastjson.JSON;
import com.hlin.sensitive.KWSeekerManage;
import com.hlin.sensitive.SensitiveWordResult;
import com.hlin.sensitive.SimpleKWSeekerProcessor;
import org.springframework.util.StringUtils;

import java.util.List;

public class SensitiveUtil {

    /**
     * 检查禁词,有则返回禁词,无返回""
     * @param text
     * @return
     */
    public static String check(String text){
        if(StringUtils.isEmpty(text)){
            return "";
        }
        // 搜索器组,构建敏感词管理器,可同时管理多个搜索器，
        KWSeekerManage kwSeekerManage = SimpleKWSeekerProcessor.newInstance();
        List<SensitiveWordResult> result = kwSeekerManage.getKWSeeker("sensitive-word").findWords(text);
        return JSON.toJSONString(result);
    }

    /**
     * 禁词拦截,以***替换
     * @return
     */
    public static String filter(String text){
        if(StringUtils.isEmpty(text)){
            return "";
        }
        // 搜索器组,构建敏感词管理器,可同时管理多个搜索器，
        KWSeekerManage kwSeekerManage = SimpleKWSeekerProcessor.newInstance();
        String result = kwSeekerManage.getKWSeeker("sensitive-word").replaceWords(text);
        return result;
    }
}
